package com.boarsoft.boar.soagov.svc.biz;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.soagov.entity.ProtocolInfo;
import com.boarsoft.boar.soagov.entity.SvcInfo;
import com.boarsoft.boar.soagov.entity.SvcProtocol;
import com.boarsoft.boar.soagov.svc.SvcProtocolBiz;
import com.boarsoft.common.Util;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("svcProtocolBiz")
public class SvcProtocolBizImpl extends SimpleBizImpl implements SvcProtocolBiz {

	@Override
	@Transactional(readOnly = true)
	public List<SvcProtocol> list(String svcId) {
		StringBuilder sb = new StringBuilder();
		sb.append("select sp from ").append(SvcProtocol.class.getName());
		sb.append(" sp, ").append(ProtocolInfo.class.getName()).append(" p,").append(SvcInfo.class.getName()).append(" s")
				.append(" where sp.protocolId = p.id and sp.svcId = s.id and s.id='").append(svcId).append("'");
		List<SvcProtocol> lt = dao.list(sb.toString());
		return lt;
	}

	@Override
	@Transactional
	public boolean save(SvcProtocol svcProtocol) {
		try {
			if (Util.strIsEmpty(svcProtocol.getId())) {
				dao.save(svcProtocol);
			} else {
				dao.update(svcProtocol);
			}
			return true;
		} catch (Exception e) {
			log.error("Failed to save svc_protocol:", e);
			return false;
		}
	}

	@Override
	@Transactional
	public void delete(String svcId, String protocolId) {
		try {
			StringBuilder sb = new StringBuilder();
			sb.append("delete from ").append(SvcProtocol.class.getName()).append(" where 1=1 ");
			if (Util.strIsNotEmpty(svcId)) {
				sb.append(" and svcId = '").append(svcId).append("'");
			}
			if (Util.strIsNotEmpty(protocolId)) {
				sb.append(" and protocolId = '").append(protocolId).append("'");
			}
			if (Util.strIsNotEmpty(protocolId) && Util.strIsNotEmpty(svcId)) {
				dao.executeUpdate(sb.toString());
			}
		} catch (Exception e) {
			log.error("delete svc_protocol error：", e);
		}
	}

	@Override
	@Transactional
	public boolean save(String[] protocolIds, String[] svcIds, boolean flag) {
		try {
			// 更新则先删除之前的数据
			if (!flag) {
				delete(svcIds);
			}
			List<SvcProtocol> svcProtocols = new ArrayList<SvcProtocol>();
			for (String svcId : svcIds) {
				for (String protocolId : protocolIds) {
					if (Util.strIsNotEmpty(protocolId)) {
						SvcProtocol svcProtocol = new SvcProtocol();
						svcProtocol.setProtocolId(protocolId);
						svcProtocol.setSvcId(svcId);
						svcProtocols.add(svcProtocol);

					}
				}
			}
			if (svcProtocols.size() > 0) {
				dao.batchSave(svcProtocols, 100);
			}
			return true;
		} catch (Exception e) {
			log.error("Failed to save svc_protocols:", e);
			return false;
		}
	}

	@Override
	@Transactional
	public void delete(String[] svcIds) {
		try {
			for (String svcId : svcIds) {
				StringBuilder sb = new StringBuilder();
				sb.append("delete from ").append(SvcProtocol.class.getName()).append(" where");
				if (Util.strIsNotEmpty(svcId)) {
					sb.append(" svcId = '").append(svcId).append("'");
					dao.executeUpdate(sb.toString());
				}
			}
		} catch (Exception e) {
			log.error("delete svc_protocol error：", e);
		}
	}
}